package com.ssm.user.config;

import com.ssm.threadpool.config.CustomNameThreadFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Configuration
public class ThreadPollConfig {

     @Bean(name = "mailThreadPool")
     public ThreadPoolExecutor getMailThreadPool() { //发送邮件的线程池

         // 创建自定义的线程工厂(用于自定义线程名)
         CustomNameThreadFactory mailFactory = new CustomNameThreadFactory("mail");
         return new ThreadPoolExecutor(
                 20, //核心线程数
                 50, //最大线程数
                 5, //空闲线程存活时间
                 TimeUnit.SECONDS, //单位
                 new LinkedBlockingDeque<>(), //阻塞队列
                 mailFactory, //添加自定义的mail线程工厂
                 new ThreadPoolExecutor.CallerRunsPolicy() //拒绝策略(由向线程池提交任务的线程来执行该任务)
         );
         //未声明线程工厂，默认为Executors.defaultThreadFactory
     }
}
